//
//  AlbumsDataSource.m
//  MantuPlay
//
//  Created by Markus Sintonen on 14.8.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AlbumsDataSource.h"
#import "TracksDataSource.h"
#import "TrackDatabase.h"
#import "Album.h"
#import "NSArrayExtensions.h"
#import "UITableViewExtensions.h"
#import "NSOperationQueueSharedQueue.h"
#import "DebuggingMacros.h"

#define ROW_HEIGHT 57.0
#define IMAGEVIEW_TAG 1
#define FIRSTLABEL_TAG 2
#define SECONDLABEL_TAG 3
#define OBJECTCOUNTLABEL_TAG 4

@implementation AlbumsDataSource

@synthesize navigationBarTitle, albumsFromArtist, albumsFromGenre;

- (id)init {
	if ((self = [super init])) {
		self.navigationBarTitle = @"Albums";
		
		showAllTracksCell = NO;
	}
	return self;
}

-(id)initWithAlbumsFromArtist:(NSString*)artist genre:(NSString*)genre {
	if ((self = [super init])) {
		if(artist) albumsFromArtist = [[NSString alloc] initWithString:artist];
		if(genre) albumsFromGenre = [[NSString alloc] initWithString:genre];
		
		self.navigationBarTitle = @"Albums";
		
		showAllTracksCell = YES;
	}
	return self;
}

-(NSArray*)databaseObjectArray {
	return albums;
}

- (NSString*)name {
	return @"Albums";
}

- (NSString*)navigationBarName {
	return navigationBarTitle;
}

-(CGFloat)rowHeight {
	return ROW_HEIGHT;
}

-(BOOL)showSectioned {
	return YES;
}

-(BOOL)extraRowAtTop {
	return (showAllTracksCell && ([albums count] > 1));
}
-(BOOL)extraRowAtBottom {
	return ([albums count] > 5);
}

-(void)loadData {
	if(!albums) {
		NSArray *tempAlbums = [[TrackDatabase sharedTrackDatabase] albumsFromArtist:albumsFromArtist genre:albumsFromGenre];
		albums = [[NSArray alloc] initWithArray:tempAlbums];
	}
}
-(void)unloadData {
	[[NSOperationQueue sharedOperationQueue] cancelAllOperations];
	
	[albums release];
	albums = nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForObject:(id)obj atIndexPath:(NSIndexPath*)indexPath {
	UIImageView *albumArtView;
	UILabel *albumNameLabel;
	UILabel *artistLabel;
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"AlbumCell"] autorelease];
		
		albumArtView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, ROW_HEIGHT-1.0, ROW_HEIGHT-1.0)] autorelease];
		albumArtView.contentMode = UIViewContentModeScaleAspectFit;
		albumArtView.tag = IMAGEVIEW_TAG;
		[cell.contentView addSubview:albumArtView];
		
		albumNameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(ROW_HEIGHT+2.0, 1.0, 320.0-ROW_HEIGHT-2.0, ROW_HEIGHT/2.0-1.0)] autorelease];
		albumNameLabel.font = [UIFont boldSystemFontOfSize:17.0];
		albumNameLabel.tag = FIRSTLABEL_TAG;
		[cell.contentView addSubview:albumNameLabel];
		
		artistLabel = [[[UILabel alloc] initWithFrame:CGRectMake(ROW_HEIGHT+2.0, ROW_HEIGHT/2.0, 320.0-ROW_HEIGHT-2.0, ROW_HEIGHT/2.0-1.0)] autorelease];
		artistLabel.font = [UIFont systemFontOfSize:14.0];
		artistLabel.textColor = [UIColor grayColor];
		artistLabel.tag = SECONDLABEL_TAG;
		[cell.contentView addSubview:artistLabel];
		
	} else {
		albumArtView = (UIImageView *)[cell.contentView viewWithTag:IMAGEVIEW_TAG];
		albumNameLabel = (UILabel *)[cell.contentView viewWithTag:FIRSTLABEL_TAG];
		artistLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
	}
	albumArtView.image = nil;
	
	Album *album = (Album*)obj;
	AlbumArtLoadOperation *albumArtLoadOperation;
	albumArtLoadOperation = [[AlbumArtLoadOperation alloc] initWithImageView:albumArtView albumName:album.title imageSize:CGSizeMake(55.0, 55.0)];
	[[NSOperationQueue sharedOperationQueue] addOperation:albumArtLoadOperation];
	[albumArtLoadOperation release];
	
	albumNameLabel.text = album.title;
	if(album.isCompilation) artistLabel.text = @"Compilation";
	else artistLabel.text = album.albumArtist;
	
	return cell;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtTopIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllTracksCell"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"AllTracksCell"] autorelease];
	}
	cell.text = @"All Songs";
	
	return cell;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForExtraRowAtBottomIndexPath:(NSIndexPath*)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ObjectCountCell"];
	UILabel *albumCountCell;
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"ObjectCountCell"] autorelease];
		
		albumCountCell = [[[UILabel alloc] initWithFrame:CGRectMake(1.0, 1.0, 320.0-1.0, ROW_HEIGHT-2.0)] autorelease];
		albumCountCell.tag = OBJECTCOUNTLABEL_TAG;
		albumCountCell.textColor = [UIColor grayColor];
		albumCountCell.textAlignment = UITextAlignmentCenter;
		[cell.contentView addSubview:albumCountCell];
	} else {
		albumCountCell = (UILabel *)[cell.contentView viewWithTag:OBJECTCOUNTLABEL_TAG];
	}
	albumCountCell.text = [NSString stringWithFormat:@"%d albums", [albums count]];
	
	return cell;
}

-(id<DatabaseObjectDataSource>)tableView:(UITableView *)tableView dataSourceForSelectedObject:(id)obj atIndexPath:(NSIndexPath *)indexPath {
	Album *album = (Album*)obj;
	TracksDataSource<DatabaseObjectDataSource> *tracksDataSource;
	tracksDataSource = [[TracksDataSource alloc] initWithTracksFromAlbum:album.title artist:albumsFromArtist genre:albumsFromGenre orderByTrackNumber:YES];
	
	tracksDataSource.navigationBarTitle = album.title;
	tracksDataSource.showTrackArtistAndAlbum = album.isCompilation;
	tracksDataSource.sectioned = NO;
	
	return [tracksDataSource autorelease];
}
-(id<DatabaseObjectDataSource>)tableView:(UITableView *)tableView dataSourceForSelectedExtraRowAtTopIndexPath:(NSIndexPath*)indexPath {
	TracksDataSource<DatabaseObjectDataSource> *tracksDataSource;
	tracksDataSource = [[TracksDataSource alloc] initWithTracksFromAlbum:nil artist:albumsFromArtist genre:albumsFromGenre orderByTrackNumber:NO];

	tracksDataSource.navigationBarTitle = @"Songs";
	tracksDataSource.showTrackArtistAndAlbum = YES;
	tracksDataSource.sectioned = YES;
	
	return [tracksDataSource autorelease];
}

- (void)dealloc {
	[albums release];
	[navigationBarTitle release];
	[albumsFromArtist release];
	[albumsFromGenre release];
	[super dealloc];
}


@end
